package com.ask.controller.test;

import com.ask.dto.Result;
import com.ask.entity.SeckillVoucher;
import com.ask.service.businessService.IBlogService;
import com.ask.service.businessService.ISecKillVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test/juc")
public class JucController {
    @Autowired
    private IBlogService blogService;
    @Autowired
    private ISecKillVoucherService voucherService;

    /**
     * 博客点赞
     * @param id
     * @return
     */
    @PostMapping
    public Result blogLike(Long id) {
        boolean success = blogService.update()
                .setSql("liked = liked + 1")
                .eq("id", id)
                .update();
        if (success) return Result.success();
        else return Result.fail("fail");
    }

    /**
     * 优惠卷秒杀
     * @param id
     * @return
     */
    @PostMapping("/buy")
    public Result seckill(Long id) {
        SeckillVoucher voucher = voucherService.getById(id);
        if (voucher.getStock() > 0) {
            boolean success = voucherService.update().setSql("stock = stock - 1").update();
            if (success)
                return Result.success("扣减成功！");
        }
        return Result.fail("扣减失败");
    }
}
